package com.zhongfeng.api.modules.utils.function;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import java.util.ArrayList;
import java.util.List;

/**
 * @author blueCat
 * @date 2023/2/3 9:21
 * @motto life is a gift
 */
public class Transfer {

    /**
     * 将“[1],[2]”转化为[1,2]的集合
     *
     * @param listStr
     * @return
     */
    public static List<Integer> transferString(String listStr) {
        List<Integer> result = new ArrayList<>();
        String[] split = listStr.split(",");
        for (String s : split) {
            s = s.substring(1, s.length());
            s = s.substring(0, s.length() - 1);
            result.add(Integer.parseInt(s));
        }
        return result;
    }

    /**
     * 将“[1],[2]”转化为[1,2]的集合
     *
     * @param listStr
     * @return
     */
    public static List<String> transferString2(String listStr) {
        List<String> result = new ArrayList<>();
        String[] split = listStr.split(",");
        for (String s : split) {
            s = s.substring(1, s.length());
            s = s.substring(0, s.length() - 1);
            result.add(s);
        }
        return result;
    }

    /**
     * 合并集合为一个String，中间
     *
     * @return
     */
    public static <T> String combineList(List<T> strList) {
        if (strList != null && strList.size() > 0){
            String result = "";
            for (T s : strList) {
                result = result + "[" + s + "],";
            }
            return result.substring(0, result.length() - 1);
        }
        return "";
    }

    /**
     * 合并集合为一个String，无[]
     *
     * @return
     */
    public static <T> String combineListNo(List<T> strList) {
        if (strList != null && strList.size() > 0){
            String result = "";
            for (T s : strList) {
                result = result + "http://" + s + ":28080,";
            }
            return result.substring(0, result.length() - 1);
        }
        return "";
    }

    /**
     * 合并集合为一个String，无[]
     *
     * @return
     */
    public static <T> String combineListNo2(List<T> strList) {
        if (strList != null && strList.size() > 0){
            String result = "";
            for (T s : strList) {
                result = result + s + ",";
            }
            return result.substring(0, result.length() - 1);
        }
        return "";
    }

    /**
     * 将价格转为中文
     * @param price
     * @return
     */
    public static String transferPrice(Double price){
        String result = "";
        String priceCN = price + "";
        char[] chars = priceCN.toCharArray();
        int count = 0;
        for (char aChar : chars) {
            if (count>0){
                if (aChar != '0'){
                    switch (count){
                        case 1:
                            result += (aChar +"毛");
                            break;
                        case 2:
                            result += (aChar + "分");
                            break;
                        case 3:
                            result += (aChar +"厘");
                            break;
                        case 4:
                            result += (aChar +"毫");
                            break;
                    }
                }
                count++;
            }
            if (aChar == '.'){
                count++;
            }
        }
        return result;
    }
}
